package controllers;

import java.util.ArrayList;
import java
.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

import model.entities.ControlTableDistInfo;
import myDaos.ControlDistDao;

@ManagedBean
@ViewScoped
public class ControlDistInfoManagedBean {
	
	private List<ControlTableDistInfo> clist;
	private String country;
	private Date from;
	private Date to;
	private String distributor;
	private boolean isMethodCalled = false;
	private List<SelectItem> distList;
	private List<String> distributors;
	private List<String> countries;
	private List<String> interfcaes;

	

	@EJB
	private ControlDistDao cdd;
	 
	@ManagedProperty("#{servicesManagedBean}")
	private ServicesManagedBean servicesManagedBean;
	
	
	
	
	public List<String> getInterfcaes() {
		return interfcaes;
	}


	public void setInterfcaes(List<String> interfcaes) {
		this.interfcaes = interfcaes;
	}



	public List<String> getCountries() {
		return countries;
	}


	public void setCountries(List<String> countries) {
		this.countries = countries;
	}


	public List<String> getDistributors() {
		return distributors;
	}


	public void setDistributors(List<String> distributors) {
		this.distributors = distributors;
	}

	public List<SelectItem> getDistList() {
		return distList;
	}


	public void setDistList(List<SelectItem> distList) {
		this.distList = distList;
	}


	public ServicesManagedBean getServicesManagedBean() {
		return servicesManagedBean;
	}


	public void setServicesManagedBean(ServicesManagedBean servicesManagedBean) {
		this.servicesManagedBean = servicesManagedBean;
	}


	public boolean isMethodCalled() {
		return isMethodCalled;
	}






	public void setMethodCalled(boolean isMethodCalled) {
		this.isMethodCalled = isMethodCalled;
	}










	public String getDistributor() {
		return distributor;
	}






	public void setDistributor(String distributor) {
		this.distributor = distributor;
	}






	public Date getFrom() {
		return from;
	}






	public void setFrom(Date from) {
		this.from = from;
	}






	public Date getTo() {
		return to;
	}






	public void setTo(Date to) {
		this.to = to;
	}






	public List<ControlTableDistInfo> getClist() {
		return clist;
	}






	public void setClist(List<ControlTableDistInfo> clist) {
		this.clist = clist;
	}






	public String getCountry() {
		return country;
	}






	public void setCountry(String country) {
		this.country = country;
	}




	public void updateList(){		
		clist = cdd.getControlDistInfoList(interfcaes,distributors,from,to);
		isMethodCalled=true;
	}
	
	public void updateDistributorsList()
	{
		//Proposed by Samy
		distList = new ArrayList<SelectItem>();
		for(int l = 0; l < countries.size(); l++){
			for(int i = 0; i < servicesManagedBean.getCountriesDistributorList().size(); i++) 
		    {  	
				if(countries.get(l)!=null && !countries.get(l).trim().equals("") && 
						  servicesManagedBean.getCountriesDistributorList().get(i).getCountry().equals(countries.get(l)))
				{
				distList.add(new SelectItem(
											 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
											 ,
											 servicesManagedBean.getCountriesDistributorList().get(i).getDistCode()
						                    )
				             );
				}	
			}
	    }
	}

	@PostConstruct
	public void init(){
		to = new Date();
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(to);
		gc.add(Calendar.DAY_OF_YEAR, -1);
		from = gc.getTime();
	}

}
